/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fpsman;

import com.jme3.animation.LoopMode;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.events.CinematicEvent;
import com.jme3.cinematic.events.CinematicEventListener;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

/**
 *
 * @author 100350
 */
public class MyMotionControl extends MotionEvent{
    
    boolean reseted;

    public boolean isReseted() {
        return reseted;
    }

    public void setReseted(boolean reseted) {
        this.reseted = reseted;
    }
    
    public MyMotionControl(Spatial spatial, MotionPath path){
        super(spatial, path);
        setDirectionType(MotionEvent.Direction.PathAndRotation);
        setRotation(new Quaternion().fromAngleNormalAxis(FastMath.PI, Vector3f.UNIT_Y));
        setLoopMode(LoopMode.DontLoop);
        setInitialDuration(path.getNbWayPoints()*3);
        addListener(new CinematicEventListener() {

            @Override
            public void onPlay(CinematicEvent cinematic) {
                
            }

            @Override
            public void onPause(CinematicEvent cinematic) {
                
            }

            @Override
            public void onStop(CinematicEvent cinematic) {
                if(!isReseted()){
                    attachPath(((MyPath)getPath()).getNext());
                    play();
                }
                else{
                    setReseted(false);
                }
            }
        });
    }
    
    public void attachPath(MyPath path){
        setPath(path);
        setInitialDuration(path.getNbWayPoints()*3);
        setSpeed(1);
    }
    
    public Vector3f getLocation(){
        return spatial.getLocalTranslation();
    }
}
